package com.hdu.clj.fitness

import android.content.Context
import android.util.Log
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.sql.Time
import java.util.*
import kotlin.concurrent.timer

class Answer {

    private val sorry = listOf<String>("我不知道你在说什么","再给你一次机会重新组织一下语言","您给翻译翻译","我网不好，要不你先挂了","你在说啥？","" +
            "你想表达什么","呵呵","大。。大。。大佬","打扰了","两情若是久长时，又岂在朝朝暮暮","欲把相思说似谁，浅情人不知","只愿君心似我心，定不负相思意",
        "春心莫共花争发，一寸相思一寸灰","天涯地角有穷时，只有相思无尽处","衣带渐宽终不悔，为伊消得人憔悴","天不老，情难绝。心似双丝网，中有千千结",
        "愿君多采撷，此物最相思","遥知兄弟登高处，遍插茱萸少一人","人有悲欢离合，月有阴晴圆缺，此事古难全。","洛阳城里见秋风，欲作家书意万重",
        "烽火连三月，家书抵万金","王师北定中原日，家祭无忘告乃翁","劝君更尽一杯酒，西出阳关无故人","孤帆远影碧空尽，唯见长江天际流","洛阳亲友如相问，一片冰心在玉壶",
        "我寄愁心与明月，随君直到夜郎西","莫愁前路无知己，天下谁人不识君","满地黄花堆积，憔悴损，如今有谁堪摘","山重水复疑无路，柳暗花明又一村",
        "沙上并禽池上暝。云破月来花弄影","沾衣欲湿杏花雨，吹面不寒杨柳风","春花秋月何时了？往事知多少","什么TM的叫做TM的惊喜")
    private val sur = listOf<String>("这么神奇吗。。","我的天哪。。","哈哈哈哈哈哈哈哈哈","是的呢！","厉害了","tql","起灰了","真的吗","太棒了","你他N的真是个人才","你了不起，你清高","这TM就不是你")
    private val many = listOf<String>("这我哪知道，我只是个初级聊天机器人","你猜啊","你猜我猜不猜","蒸羊羔,蒸熊掌,蒸鹿尾儿, 烧花鸭,烧雏鸡儿,烧子鹅 ,卤煮咸鸭,酱鸡,腊肉，一盆米饭我够了")


    fun answer(str:String,context: Context):String{

        var ans =sorry[(0..sorry.size-1).random()]
        var strs = Regex(str)

        val case0 = str.contains("我")
        val case1 = strs.containsMatchIn("你是")
        val case2 = str.contains("谁")
        val case3 = str.contains("是")
        val case4 = strs.containsMatchIn("多少")
        val case5 = str.contains("天气")
        val case6 = str.contains("为什么")
        val case7 = str.contains("啊")||str.contains("哇")||str.contains("呀")
        val case8 = str.contains("你好")
        val case9 = str.contains("你是")
        val case10 = str.contains("想")

        val dbTalkHelper = talkDatabase(context,"Talk.db",1)
        val dbTalk = dbTalkHelper.writableDatabase

        val sumTalk = dbTalk.rawQuery("select max(id) from Talk", null)
        var length = 0
        if (sumTalk.moveToFirst()) {
            length = sumTalk.getInt(0)
        }
        Log.d("curdata","len ; $length")
        if (length>=10&&(0..10).random()>5) {

            val index = (0..length).random()
            val randomTalk =
                dbTalk.rawQuery("select * from Talk where id = ?", arrayOf(index.toString()))
            if (randomTalk.moveToFirst()) {
                return randomTalk.getString(randomTalk.getColumnIndex("content"))
            }
        }
        else{
            if (case0&&case2){
                return "你是 ${LoadAndSave().load("userName",context)},不是吗"
            }
            else if (case1&&case2||case9){
                return "我是聊天机器人，编号250 （目前等级：青铜）"
            }
            else if (case4||case6){
                return many[(0..many.size-1).random()]
            }
            else if (case5){
                return "今夜阳光明媚，今夜多云转晴。。。"
            }
            else if (case8) {
                return "大家好才是真的好"
            }
            else if (case10){
                return "那你是想多了！"
            }
            else if (case7) {
                return sur[(0..sur.size - 1).random()]
            }
            else{
                return ans
            }
        }
        return ""
    }

}